package com.wink_172.library.view

import android.content.Context
import android.os.CountDownTimer
import android.util.AttributeSet
import android.util.Log
import android.widget.TextView
import androidx.appcompat.widget.AppCompatTextView
import com.wink_172.library.R
import com.wink_172.library.model.Constants
import com.wink_172.library.utils.DateUtil
import com.wink_172.library.utils.MMKVTool

/**
 *
 * 这个还是存在问题的出现一秒钟重复回调的问题 太绕自己都看不懂  需要优化和简洁
 *
 * 计时器  适用于验证码
 *
 * 使用方法
 *  android:id="@+id/count_down_view"
 *
 *       (binding!!.countDownView as CountTimeView).setCallback((object :CountTimeView.ICallback{
override fun onSendEvent(event: Int,vararg args: Any) {
when(event){
Constants.EVENT_01->{
args[0] as kotlin.Long?
}

Constants.EVENT_02->{

}
}

}
}))



(binding!!.countDownView as CountTimeView).init( ""+System.currentTimeMillis())
(binding!!.countDownView as CountTimeView).saveCurrentTime();
(binding!!.countDownView as CountTimeView).startCutDownTime(15* 1000)
(binding!!.countDownView as CountTimeView).stopCutDownTime();
 *
 */
open class CountTimeView : AppCompatTextView {
    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context,
        attrs,
        defStyleAttr
    ) {
    }

    fun setCallback(callback: ICallback?) {
        mCallback = callback
    }

    open  var mCallback: ICallback? = null

    interface ICallback {
        fun onSendEvent(event: Int,vararg args: Any)
    }



    override fun onFinishInflate() {
        super.onFinishInflate()
        initView()
    }

    private fun initView() {}
    private var mParams: String? = null

    /**
     * 初始化
     */
    open fun init(params: String?) {
        mParams = params
        reloadLastGetCodeTime(params)
    }

    open fun saveCurrentTime() {
        MMKVTool.setLong(org.xutils.x.app(), mParams, System.currentTimeMillis())
    }

    /**
     * 更新上次获取短信时间
     * params  key
     */
    private fun reloadLastGetCodeTime(params: String?) {
        val lastTime = MMKVTool.getLong(org.xutils.x.app(), params, java.lang.Long.valueOf(0))
        if (lastTime == 0L) {
            isEnabled = true
            isClickable = true
        } else {
            val timeGap = DateUtil.getDescriptionTimeFromTimestamp02(lastTime)
            if (timeGap > TIME_SAPCE) {
                isEnabled = true
                isClickable = true
            } else {
                isEnabled = false
                isClickable = false
                startCutDownTime((TIME_SAPCE - timeGap) * 1000)
            }
        }
    }

    private var countDownTimer: CountDownTimer? = null
    open fun startCutDownTime(millisInfuture: Long) {
        countDownTimer = object : CountDownTimer(millisInfuture, 1000) {
            override fun onTick(millisUntilFinished: Long) {
                val input = resources.getString(R.string.wink129)
                val output = String.format(input, "" + millisUntilFinished / 1000)
                text = output

//                Log.e(TAG, "onTick: ====>>"+input )
                mCallback?.let {
                    mCallback?.onSendEvent(Constants.EVENT_01,millisUntilFinished);

                }
            }

            override fun onFinish() {
                Log.e(TAG, "onFinish: ====>>" )
                val input1 = resources.getString(R.string.wink130)
                text = input1
                isEnabled = true
                isClickable = true
                mCallback?.let {
                    mCallback?.onSendEvent(Constants.EVENT_02);

                }
            }
        }
        countDownTimer?.start()
    }

    private  val TAG = "CountTimeView"

    /**
     * 销毁
     */
   open fun stopCutDownTime() {
        if (countDownTimer != null) {
            countDownTimer!!.cancel()
        }
        countDownTimer = null
    }

    companion object {
        const val TIME_SAPCE = 60 //获取短信的间隔60秒
    }
}